<?php
$SIZE = 10;

//latest articles
$page = MiscUtils::getParam('p', 1);
$order = MiscUtils::getParam('o', 't.date');
$queue = MiscUtils::getParam('q', 'DESC');
$cond_vals = new stdClass();
$cond_vals->c = 't.tag_ids LIKE :v1';
$cond_vals->v = array(':v1' => "%|$cat_id|%");
$opts = new stdClass();
$opts->select_expr = 'Distinct t.id, t.title';
$updateds = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'art_post', $cond_vals, $order, $queue, $page, $SIZE, $opts)->d;

//popular articles
$page = MiscUtils::getParam('p', 1);
$order = MiscUtils::getParam('o', 't.visitors');
$queue = MiscUtils::getParam('q', 'DESC');
$cond_vals = new stdClass();
$cond_vals->c = 't.tag_ids LIKE :v1';
$cond_vals->v = array(':v1' => "%|$cat_id|%");
$opts = new stdClass();
$opts->select_expr = 'Distinct t.id, t.title';
$hots = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'art_post', $cond_vals, $order, $queue, $page, $SIZE, $opts)->d;

//browsed articles
if (isset($_COOKIE["news_read"])) {
  $_SESSION["news_read"] = json_decode($_COOKIE["news_read"]);
}
  
if (!isset($_SESSION["news_read"])) {
  $_SESSION["news_read"] = array();
}

//relative articles
if (isset($cat_id)) {
//	$cond_vals = new stdClass();
//	$cond_vals->c = 't.id = :v1';
//	$cond_vals->v = array(':v1' => $art_id);
//	$opts = new stdClass();
//	$opts->select_expr = 'Distinct t.tag_ids';
//	$tag_str = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'art_post', $cond_vals, NULL, NULL, 1, 1, $opts)->d[0]->tag_ids;
//	
//	$tags = explode('|', $tag_str);
//	$ids = array();
//	foreach ($tags as $tag) {
//	  if (is_numeric($tag)) {
//	    $ids[] = $tag;
//	  }
//	}
	
	$page = MiscUtils::getParam('p', 1);
	$order = MiscUtils::getParam('o', 't.id');
	$queue = MiscUtils::getParam('q', 'DESC');
	$cond_vals = new stdClass();
//	$cond = '';
//	$vals = array();
	
//	foreach ($ids as $idx => $id) {
//	  $cond .= 't.tag_ids like :v' . ($idx + 1);
//	  if ($idx < (count($ids) - 1)) {
//	    $cond .= ' OR ';
//	  }
//	  $vals[':v' . ($idx + 1)] = '%|' . $id . '|%';
//	}
	$cond_vals = new stdClass();
//	$cond_vals->c = $cond;
//	$cond_vals->v = $vals;
	$cond_vals->c = 't.tag_ids like :v1';
	$cond_vals->v = array(':v1' => '%|' . $cat_id . '|%');
	$opts = new stdClass();
	$opts->select_expr = 'Distinct t.id, t.title';
//	if ($cond_vals->c != '') {
	  $relas = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'art_post', $cond_vals, $order, $queue, $page, $SIZE, $opts)->d;
//	}
  
}
?>
<style>
.linkbtn {
  border: none;
  background: #fff;
  padding: 0;
  text-decoration: underline;
  cursor: pointer;
  font-size: 12px;
}

.title {
  padding-left: 10px;
  vertical-align: middle;
  font: 12px/1.5 Verdana, Arial, Helvetica, sans-serif, "宋体";
  color: #333;
  background: #f7f7f7 url(images/title_bg.png) repeat-x;
  background-position: 0 -476px;
  border: 1px solid #ddd;
  line-height: 31px;
  height: 30px;
}

.content {
  margin: 0px;
  border: solid 1px #d5d5d5;
  border-top: none;
  background: #fcfcfc url(images/content_bg.gif) repeat-x;
}

.content ul {
  list-style: none;
  padding: 0px 8px 2px 8px;
  margin: 0px;
}

.content ul li {
  width: 100%;
  text-align: left;
  vertical-align: middle;
  background: url(images/bullet.gif) no-repeat left center;
  font: 12px/1.6 Verdana, Arial, Helvetica, sans-serif, "宋体";
  padding: 2px 0px 2px 8px;
  list-style: none;
}

.content ul li a {
  color: #333;
  text-decoration: none;
}
.content ul li a:hover{
  text-decoration: underline;
  color: #666;
}
</style>
<div style="width: 100%;">
	  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="title">
	  <tr>
	    <td><strong>Latest Articles</strong></td>
	  </tr>
	</table>
	<table width="100%" cellpadding=0 cellspacing=0 class="content">
	  <tr><td>
	   <ul>
	  <?php foreach ($updateds as $idx => &$item) { ?>
	  <li><a href="#" title="<?php echo $item->title; ?>" onclick="parent.location.href='<?php echo 'http://' . URL . '?i=' . $page_id . "&script=index_detail&cat_id=$cat_id&art_id=$item->id"; ?>';"><?php echo MiscUtils::getShortName($item->title, 40); ?><a></li>
	  <?php } ?>
	  <?php if (count($updateds) == 0) { ?>
	  <li>None</li>
	  <?php } ?>
	  </ul>
	  </td></tr>
	</table>
  <div>&nbsp;</div>
  <?php if ($script == 'index_detail') { ?>
  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="title">
	  <tr>
	    <td><strong>Related Articles</strong></td>
	  </tr>
	</table>
	<table width="100%" cellpadding=0 cellspacing=0 class="content">
	  <tr><td>
	   <ul>
	  <?php foreach ($relas as $idx => &$item) { ?>
	  <li><a href="#" title="<?php echo $item->title; ?>" onclick="parent.location.href='<?php echo 'http://' . URL . '?i=' . $page_id . "&script=index_detail&cat_id=$cat_id&art_id=$item->id"; ?>';"><?php echo MiscUtils::getShortName($item->title, 40); ?><a></li>
	  <?php } ?>
	  <?php if (count($relas) == 0) { ?>
	  <li>None</li>
	  <?php } ?>
	  </ul>
	  </td></tr>
	</table>
  <?php } else { ?>
  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="title">
	  <tr>
	    <td><strong>Popular Articles</strong></td>
	  </tr>
	</table>
	<table width="100%" cellpadding=0 cellspacing=0 class="content">
	  <tr><td>
	   <ul>
	  <?php foreach ($hots as $idx => &$item) { ?>
	  <li><a href="#" title="<?php echo $item->title; ?>" onclick="parent.location.href='<?php echo 'http://' . URL . '?i=' . $page_id . "&script=index_detail&cat_id=$cat_id&art_id=$item->id"; ?>';"><?php echo MiscUtils::getShortName($item->title, 40); ?><a></li>
	  <?php } ?>
	  <?php if (count($hots) == 0) { ?>
	  <li>None</li>
	  <?php } ?>
	  </ul>
	  </td></tr>
	</table>
	<?php } ?>
  <div>&nbsp;</div>
  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="title">
	  <tr>
	    <td><strong>Browsed Articles</strong></td>
	  </tr>
	</table>
	<table width="100%" cellpadding=0 cellspacing=0 class="content">
	  <tr><td>
	   <ul>
	  <?php foreach ($_SESSION["news_read"] as $idx => &$item) { ?>
	  <li><a href="#" title="<?php echo $item->title; ?>" onclick="parent.location.href='<?php echo 'http://' . URL . '?i=' . $page_id . "&script=index_detail&cat_id=$item->_cat_id&art_id=$item->id"; ?>';"><?php echo MiscUtils::getShortName($item->title, 40); ?><a></li>
	  <?php } ?>
	  </ul>
	  </td></tr>
	  <tr>
      <td style="text-align: right;">
        <form action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
          <input name="action" value="clear" style="display: none;"></input>
          <input name="to" value="<?php echo MiscUtils::curPageURL(); ?>" style="display: none;"></input>
          <input type='submit' value="Clear All" class="linkbtn"></input>
        </form>
      </td>
    </tr>
  </table>
</div>